package com.study.kafka.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFutureCallback;

/**
 * @author flwu
 * @date 2020/3/19 7:12 PM
 * @MOTTO 保持空杯和敬畏
 */
@Service
@EnableScheduling
public class ProductService {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Scheduled(fixedRate = 1000 * 5)
    public void send() {
        String message = Long.toString(System.currentTimeMillis()) + ": 发送了消息";
        kafkaTemplate.send("lubai", message).addCallback(new ListenableFutureCallback() {
            @Override
            public void onFailure(Throwable throwable) {
                System.out.print("send success");
            }

            @Override
            public void onSuccess(@Nullable Object o) {
                System.out.println("send fail");
            }
        });
    }
}
